const router = require('express').Router()
const Query = require('./poolfun')
const {mySecret} = require('../functions/commonFun')

router.get('/login', (req, res) => {
    let {phone, password} = req.query
    let sql = `select * from users where name=${phone} and status=1`
    Query(sql).then(rows => {
        if (rows.length < 1) {
            res.send({
                msg: `${phone} 未注册。`,
                status: 0,
            })
        }
        if (rows[0].password !== mySecret(password)) {
            res.send({
                msg: `密码不正确`,
                status: 0,
            })
        }
        res.cookie('currentLoginUser', phone, { maxAge: 120000 });
        return rows;
    }).then(rows => {
        let upSql = `update users set login_time=${new Date().getTime()} where id=${rows[0].id}`;
        Query(upSql).then(response => {
            res.send({
                msg: '登录成功。',
                status: 1,
            })
        })
    }).catch(err => {
        res.send(err)
    })
})

module.exports = router;